
/* 
 * naming rule:
 * cv_checkxxx
 */
/* check function */
function cv_checkIsNull(id, advice_id, descName, params, error_list, configure) {
	
	var value = CommonValidateUtil.c$(id).value;
	if(CommonValidateUtil.trim(value) == "") {
		
		error_list.push({"advice_id" : advice_id, 
			error_msg: CommonValidateUtil.format_msg(CommonValidateUtil.get_msg('cv_checkIsNull', configure), [descName])});
	}
	return error_list;
}

function cv_checkLengthRange(id, advice_id, descName, params, error_list, configure) {

	var value = CommonValidateUtil.c$(id).value;
	var min = params[0];
	var max = params[1];
	
	var length = 0;
	if(value != null) {
		value = CommonValidateUtil.trim(value);
		length = value.length;
	}
	
	if(length > max || length < min) {
		error_list.push({"advice_id" : advice_id, 
			error_msg: CommonValidateUtil.format_msg(CommonValidateUtil.get_msg('cv_checkLengthRange', configure), [descName, min, max])});
	}
	return error_list;
}

function cv_checkMaxLengthRange(id, advice_id, descName, params, error_list, configure) {

	var value = CommonValidateUtil.c$(id).value;
	var max = params[0];
	
	var length = 0;
	if(value != null) {
		value = CommonValidateUtil.trim(value);
		length = value.length;
	}
	
	if(length > max) {
		error_list.push({"advice_id" : advice_id, 
			error_msg: CommonValidateUtil.format_msg(CommonValidateUtil.get_msg('cv_checkMaxLengthRange', configure), [descName, max])});
	}
	return error_list;
}

function cv_checkIsNumber(id, advice_id, descName, params, error_list, configure) {
	
	var number = CommonValidateUtil.c$(id).value;
	if(number == null || isNaN(number)){
		error_list.push( {advice_id: advice_id, 
			error_msg: CommonValidateUtil.format_msg(CommonValidateUtil.get_msg('cv_checkIsNumber', configure), [descName])} );
	}
	return error_list;
}
function cv_checkIsInt(id, advice_id, descName, params, error_list, configure) {
	var number = CommonValidateUtil.c$(id).value;
	var pattern = /^\d+$/;
	if(number == null || !pattern.test(number)) {
		error_list.push( {advice_id: advice_id, 
			error_msg: CommonValidateUtil.format_msg(CommonValidateUtil.get_msg('cv_checkIsInt', configure), [descName])} );
	}
	
	return error_list;
}
function cv_checkIsMoreThanZero(id, advice_id, descName, params, error_list, configure) {

	var number = CommonValidateUtil.c$(id).value;
	if(number == null || isNaN(number) || isNaN(parseFloat(number)) ||
		parseFloat(number) <= 0){	
		error_list.push( {advice_id: advice_id, 
			error_msg: CommonValidateUtil.format_msg(CommonValidateUtil.get_msg('cv_checkIsMoreThanZero', configure), [descName])} );
	}
	return error_list;
}
function cv_checkIsCheckBoxSelect(id, advice_id, descName, params, error_list, configure) {

	var checkboxes = CommonValidateUtil.cname$(id);
	var checked = false;
	for(var i = 0; i < checkboxes.length; i++) {
		if(checkboxes[i].checked) {
			checked = true;
			break;
		}
	}
	if(!checked) {
		error_list.push( {advice_id: advice_id, 
			error_msg: CommonValidateUtil.format_msg(CommonValidateUtil.get_msg('cv_checkIsCheckBoxSelect', configure), [descName])} );
	}
	return error_list;
}
function cv_checkSelectCheckBoxCount(id, advice_id, descName, params, error_list, configure) {
	
	var maxCount = params[0];
	var checkboxes = CommonValidateUtil.cname$(id);
	var count = 0;
	for(var i = 0; i < checkboxes.length; i++) {
		if(checkboxes[i].checked) {
			count++;
		}
	}
	if(count > maxCount) {
		error_list.push( {advice_id: advice_id, 
			error_msg: CommonValidateUtil.format_msg(CommonValidateUtil.get_msg('cv_checkSelectCheckBoxCount', configure), [maxCount, descName])} );
	}
	return error_list;
}
function cv_checkIsImg(id, advice_id, descName, params, error_list, configure) {
	var imgName = CommonValidateUtil.c$(id).value;
	if(imgName == null) imgName = '';
	var img = imgName.split(".");
	var extName = img[img.length - 1];
	
	var special_ext_name = ["JPG", "BMP", "JPEG", "GIF", "PNG", "ICO"];
	
	if(params != null && params.length > 0) {
		special_ext_name = params;
	}
	
	if(!check_ext_name(extName, special_ext_name)) {
		
		error_list.push( {advice_id: advice_id, 
			error_msg: CommonValidateUtil.format_msg(CommonValidateUtil.get_msg('cv_checkIsImg', configure), [descName])} );
	}
	return error_list;
	
	function check_ext_name(extName, special_ext_name) {
		
		for(var i = 0; i < special_ext_name.length; i++) {
			
			if(extName.toUpperCase() == special_ext_name[i].toUpperCase()) {
				return true;
			}
		}
		return false;
	}
	
}

// 目前只支持 yyyy, mm, dd
function cv_checkDateFormat(id, advice_id, descName, params, error_list, configure) {
	
	var format = params[0];
	var pattern = /yyyy([^m]*)mm([^d]*)dd/;
	var match = pattern.exec(format);
	var year_split = match[1];
	var month_split = match[2];
	
	var validate_pattern = new RegExp("\\d{4}" + year_split + "\\d{1,2}" + month_split + "\\d{1,2}");
	var value = CommonValidateUtil.c$(id).value;
	if(value == null) value = '';
	
	if(!validate_pattern.test(value)) {
		
		error_list.push( {advice_id: advice_id, 
			error_msg: CommonValidateUtil.format_msg(CommonValidateUtil.get_msg('cv_checkDateFormat', configure), [descName])} );
	}
	
	return error_list;
}

/* check function end */